using System;
using System.Collections.Generic;
using System.Text;
using IGGCore;

namespace IGGConsole
{
    public class ModeDetailedGameFormatter : ModeGameFormatterBase
    {
        #region IModeFormatter Members

        /// <summary>
        /// Format a detailed game
        /// </summary>
        /// <param name="board"></param>
        public override void FormatMode(Scoreboard board)
        {
            Game game = board.SelectedGame;
            if (game == null)
            {
                return;
            }
            if (game.Status.Indicator.Category == Indicator.IndicatorCategoy.WARMUP
                || game.Status.Indicator.Category == Indicator.IndicatorCategoy.NEXT)
            {
                ShowGamePreview(game);
            }
            else if (game.Linescore != null)
            {
                ShowDetailedBoxscore(game);
                Console.WriteLine();
                if (game.Status.Indicator.IsInProgress)
                {
                    Console.WriteLine(" Welcome to " + game.Stadium.Name + ".");
                }
                ShowTeamsWins(game);
                Console.WriteLine();
            }

            if (game.Status.Indicator.IsInProgress)
            {
                string battingTeam = game.AwayTeam.Abbrv;
                string pitchingTeam = game.HomeTeam.Abbrv;
                if (game.Status.InningTop == false)
                {
                    battingTeam = game.HomeTeam.Abbrv;
                    pitchingTeam = game.AwayTeam.Abbrv;
                }

                int len = game.Status.CurrentBatter.FullName.Length;
                if (game.Status.CurrentPitcher.FullName.Length > len)
                {
                    len = game.Status.CurrentPitcher.FullName.Length;
                }
                len += 2;

                Console.WriteLine(" " + "Batting  "
                    + ToolBox.GetFormattedStringPadRight(battingTeam, 4)
                    + ToolBox.GetFormattedStringPadRight(game.Status.CurrentBatter.FullName, len)
                    + game.Status.CurrentBatter.Hits + "/"
                    + game.Status.CurrentBatter.Ab + " "
                    + game.Status.CurrentBatter.AVG + " "
                    + game.Status.CurrentBatter.Homeruns + " hr "
                    + game.Status.CurrentBatter.RBI + " rbi"
                    );
                Console.WriteLine(" " + "Pitching "
                    + ToolBox.GetFormattedStringPadRight(pitchingTeam, 4)
                    + ToolBox.GetFormattedStringPadRight(game.Status.CurrentPitcher.FullName, len)
                    + game.Status.CurrentPitcher.IP + " "
                    + game.Status.CurrentPitcher.ERA + " "
                    + game.Status.CurrentPitcher.Wins + "-"
                    + game.Status.CurrentPitcher.Losses);
                Console.WriteLine();
                if (game.Status.isInningStart())
                {
                    Console.WriteLine(" This is the start of the inning.");
                }
                else
                {
                    StringFormatter.FormatLongText(FormatPBP(game.Status.LastPbp));
                }
                Console.WriteLine();
                ShowLoadedBases(game);
                Console.WriteLine();
                ShowDeckHole(game, battingTeam);
            }
            else if (game.Status.Indicator.IsTerminated)
            {
                Console.Write(" W: " + game.Status.WinningPitcher.PartialName);
                Console.Write(" (" + game.Status.WinningPitcher.Wins + "-"
                    + game.Status.WinningPitcher.Wins + ", " + game.Status.WinningPitcher.ERA + ");");
                Console.Write(" L: " + game.Status.LosingPitcher.PartialName);
                Console.Write(" (" + game.Status.LosingPitcher.Wins + "-"
                    + game.Status.LosingPitcher.Wins + ", " + game.Status.LosingPitcher.ERA + ")");
                Console.WriteLine();
                if (game.Status.SavingPitcher != null && game.Status.SavingPitcher.Saves > 0)
                {
                    Console.Write(" S: " + game.Status.SavingPitcher.PartialName);
                    Console.Write(" (" + game.Status.SavingPitcher.Saves + ")");
                    Console.WriteLine();
                }

                ShowHomeRuns(game);
            }
            else if (game.Status.Indicator.IsInterrupted)
            {
                Console.WriteLine(" It is currently raining at " + game.Stadium.Name);
            }
            if (game.Linescore != null && (game.Status.Indicator.IsInProgress || game.Status.Indicator.IsInterrupted
                || game.Status.Indicator.IsTerminated))
            {
                Console.WriteLine("");
                Player[] stars = ThreeStarsCalculator.GetThreeStarPlayers(game, false);
                Console.WriteLine(" First Star  : " + ToolBox.GetFormattedStringPadRight(stars[0].TeamAbbr + " " +stars[0].LastName, 15) + stars[0].GameLines[0]);
                Console.WriteLine(" Second Star : " + ToolBox.GetFormattedStringPadRight(stars[1].TeamAbbr + " " + stars[1].LastName, 15) + stars[1].GameLines[0]);
                Console.WriteLine(" Third Star  : " + ToolBox.GetFormattedStringPadRight(stars[2].TeamAbbr + " " + stars[2].LastName, 15) + stars[2].GameLines[0]);
            }
        }

        private void ShowDetailedBoxscore(Game game)
        {
            int innCount = game.Status.Inning;
            string intro = "TOP OF THE ";
            if (game.Status.InningTop == false)
            {
                intro = "BOTTOM OF THE ";
            }
            if (game.Status.Indicator.IsInProgress)
            {
                Console.Write(" " + ToolBox.GetFormattedStringPadRight(intro + game.Status.Inning, 18));
            }
            else
            {
                Console.Write(" " + ToolBox.GetFormattedStringPadRight(" ", 18));
            }

            for (int i = 1; i <= game.Status.Inning; i++)
            {
                Console.Write(i + "  ");
            }
            Console.WriteLine("  R  H  E   B  S  O");

            Console.Write(" " + ToolBox.GetFormattedStringPadRight(game.AwayTeam.City, 18));
            int remainingInning = game.Status.Inning;
            for (int i = 0; i < innCount; i++)
            {
                int pad = 3;
                if (i >= 9)
                {
                    pad = 4;
                }
                if (game.Linescore.Innings[i].Away > -1)
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Away.ToString(), pad));
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight("0", pad));
                }
                remainingInning--;
            }
            if (remainingInning > 0)
            {
                Console.Write(ToolBox.GetFormattedStringPadRight(" ", 3 * remainingInning));
            }
            Console.Write("  ");
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Runs.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Hits.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Errors.ToString(), 3));
            if (game.Status.InningTop == true && game.Status.Indicator.IsInProgress)
            {
                Console.Write(" " + game.Status.Strikes + "  " + game.Status.Balls + "  "
                    + game.Status.Outs);
            }
            Console.WriteLine();
            Console.Write(" " + ToolBox.GetFormattedStringPadRight(game.HomeTeam.City, 18));
            remainingInning = game.Status.Inning;
            for (int i = 0; i < innCount; i++)
            {
                int pad = 3;
                if (i >= 9)
                {
                    pad = 4;
                }
                if (game.Linescore.Innings[i].Home > -1)
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Home.ToString(), pad));
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(" ", pad));
                }
                remainingInning--;
            }
            if (remainingInning > 0)
            {
                Console.Write(ToolBox.GetFormattedStringPadRight(" ", 3 * remainingInning));
            }
            Console.Write("  ");
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Runs.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Hits.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Errors.ToString(), 3));
            if (game.Status.InningTop == false && game.Status.Indicator.IsInProgress)
            {
                Console.Write(" " + game.Status.Strikes + "  " + game.Status.Balls + "  "
                    + game.Status.Outs);
            }
            Console.WriteLine();
        }


        #endregion
    }
}
